﻿@page "/scripts/{activeTab?}"
@inherits AuthComponentBase
@using System.Collections
@inject IDataService DataService 

<CascadingValue Value="this">
    <TabControl InitialActiveTab="@(ActiveTab ?? "saved-scripts")">
        <TabHeaders>
            <TabHeader Name="saved-scripts" NavigationUri="/scripts/saved-scripts">
                Saved Scripts
            </TabHeader>
            <TabHeader Name="run-script" NavigationUri="/scripts/run-script">
                Run Script
            </TabHeader>
            <TabHeader Name="script-schedules" NavigationUri="/scripts/script-schedules">
                Script Schedules
            </TabHeader>
        </TabHeaders>
        <TabContents>
            <TabContent Name="saved-scripts">
                <SavedScripts />
            </TabContent>
            <TabContent Name="run-script">
                <RunScript />
            </TabContent>
            <TabContent Name="script-schedules">
                <ScriptSchedules />
            </TabContent>
        </TabContents>
    </TabControl>
</CascadingValue>


@code {

    [Parameter]
    public string ActiveTab { get; set; }

    public bool ShowOnlyMyScripts { get; set; } = true;

    public List<ScriptTreeNode> TreeNodes { get; } = new();


    public string GetItemIconCss(ScriptTreeNode viewModel)
    {
        if (viewModel.ItemType == TreeItemType.Folder)
        {
            return "oi oi-folder text-warning";
        }
        return "oi oi-script text-success";
    }

    public async Task RefreshScripts()
    {
        TreeNodes.Clear();

        var allScripts = await DataService.GetSavedScriptsWithoutContent(User.Id, User.OrganizationID);

        foreach (var script in allScripts)
        {
            if (ShowOnlyMyScripts &&
                script.CreatorId != User.Id &&
                !script.IsPublic)
            {
                continue;
            }

            var root = BuildFolderPath(script.FolderPath);
            root.Add(new ScriptTreeNode()
            {
                Name = script.Name,
                Script = script,
                ItemType = TreeItemType.Item
            });
        }

        TreeNodes.Sort((a, b) =>
        {
            if (a.ItemType != b.ItemType)
            {
                return Comparer.Default.Compare(a.ItemType, b.ItemType);
            }

            return Comparer.Default.Compare(a.Name, b.Name);
        });
    }

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();
        await RefreshScripts();
    }


    private List<ScriptTreeNode> BuildFolderPath(string folderPath)
    {
        var root = TreeNodes;

        if (!string.IsNullOrWhiteSpace(folderPath))
        {
            var paths = folderPath.Split("/", StringSplitOptions.RemoveEmptyEntries);
            for (var i = 0; i < paths.Length; i++)
            {
                var existingParent = root.Find(x => x.Name == paths[i]);

                if (existingParent is null)
                {
                    var newItem = new ScriptTreeNode()
                    {
                        Name = paths[i],
                        ItemType = TreeItemType.Folder
                    };
                    root.Add(newItem);
                    root = newItem.ChildItems;
                }
                else
                {
                    root = existingParent.ChildItems;
                }
            }
        }

        return root;
    }
}
